package base

import (
	"context"
	"encoding/json"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/area"
	"git.myscrm.cn/golang/stark/v4"
)

type baseAreaServer struct {
	areaService area.AreaServiceIface
}

func NewBaseAreaServer(areaService area.AreaServiceIface) basePb.BaseAreaServiceServer {
	return &baseAreaServer{
		areaService: areaService,
	}
}

// AreaTrees 区域树型结构
func (s *baseAreaServer) AreaTrees(ctx context.Context, req *basePb.AreaTreesRequest) (*basePb.AreaTreesResponse, error) {
	response := &basePb.AreaTreesResponse{
		AreaJsonStr: "",
	}

	areaTrees, err := s.areaService.AreaTree(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "AreaTrees-error: %+v", err)
		return response, errcode.CTogRPCError(err)
	}

	areaBytes, err := json.Marshal(areaTrees)
	if err != nil {
		stark.Logger.Infof(ctx, "AreaTrees-error: %+v", err)
		return response, errcode.CTogRPCError(err)
	}

	response.AreaJsonStr = string(areaBytes)

	return response, nil
}
